package cn.lngaosu.lwyd.module.integration.controller.admin.weixinusernum;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.integration.controller.admin.weixinusernum.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weixinusernum.WeixinUserNumDO;
import cn.lngaosu.lwyd.module.integration.service.weixinusernum.WeixinUserNumService;

@Tag(name = "管理后台 - 微信公众号用户数量")
@RestController
@RequestMapping("/integration/weixin-user-num")
@Validated
public class WeixinUserNumController {

    @Resource
    private WeixinUserNumService weixinUserNumService;

    @PostMapping("/create")
    @Operation(summary = "创建微信公众号用户数量")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user-num:create')")
    public CommonResult<Long> createWeixinUserNum(@Valid @RequestBody WeixinUserNumSaveReqVO createReqVO) {
        return success(weixinUserNumService.createWeixinUserNum(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新微信公众号用户数量")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user-num:update')")
    public CommonResult<Boolean> updateWeixinUserNum(@Valid @RequestBody WeixinUserNumSaveReqVO updateReqVO) {
        weixinUserNumService.updateWeixinUserNum(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除微信公众号用户数量")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('integration:weixin-user-num:delete')")
    public CommonResult<Boolean> deleteWeixinUserNum(@RequestParam("id") Long id) {
        weixinUserNumService.deleteWeixinUserNum(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微信公众号用户数量")
                @PreAuthorize("@ss.hasPermission('integration:weixin-user-num:delete')")
    public CommonResult<Boolean> deleteWeixinUserNumList(@RequestParam("ids") List<Long> ids) {
        weixinUserNumService.deleteWeixinUserNumListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信公众号用户数量")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user-num:query')")
    public CommonResult<WeixinUserNumRespVO> getWeixinUserNum(@RequestParam("id") Long id) {
        WeixinUserNumDO weixinUserNum = weixinUserNumService.getWeixinUserNum(id);
        return success(BeanUtils.toBean(weixinUserNum, WeixinUserNumRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信公众号用户数量分页")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user-num:query')")
    public CommonResult<PageResult<WeixinUserNumRespVO>> getWeixinUserNumPage(@Valid WeixinUserNumPageReqVO pageReqVO) {
        PageResult<WeixinUserNumDO> pageResult = weixinUserNumService.getWeixinUserNumPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeixinUserNumRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信公众号用户数量 Excel")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user-num:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeixinUserNumExcel(@Valid WeixinUserNumPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeixinUserNumDO> list = weixinUserNumService.getWeixinUserNumPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信公众号用户数量.xls", "数据", WeixinUserNumRespVO.class,
                        BeanUtils.toBean(list, WeixinUserNumRespVO.class));
    }

}